"""
python爬取4k超清画质手机壁纸

模块：
    requests   >>> pip install requests
    parsel     >>> pip install parsel   数据解析
    os

- 数据来源：
    https://m.bcoderss.com/tag/4k/page/2/
"""
import requests  # 数据请求
import re  # 正则表达式
import os

fileName = 'img/'

if not os.path.exists(fileName):
    os.makedirs(fileName)

url = 'https://m.bcoderss.com/tag/4k/page/2/'

headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
                  "Chrome/54.0.2840.99 Safari/537.36 "
}

response = requests.post(url=url, headers=headers)
href = re.findall('<li><a target="_blank" href="(.*?)"', response.text)[2:]
for index in href:
    response_1 = requests.get(url=index, headers=headers)
    title = re.findall('<title>(.*?)</title>', response_1.text)[0]
    img_url = re.findall('<img alt=".*?" title=".*?" src="(.*?)"', response_1.text)[0]
    img_content = requests.get(url=img_url, headers=headers).content
    with open(fileName + title + '.jpg', mode='wb') as f:
        f.write(img_content)
        print('图片保存成功：', title)
    # print(response_1.text)
    # print(index)
    # print(title)
    # print(img_url)
    # break
